package shop.up.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import shop.up.model.user.UserAddress;
import shop.up.model.user.UserInfo;
import shop.up.user.mapper.UserAddressMapper;
import shop.up.user.mapper.UserInfoMapper;
import shop.up.user.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhh
 * @date 2023/4/12 16:58
 */
@Service
public class UserServiceImpl implements UserService {

    // 调用mapper 层
    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> findUserAddressListByUserId(String userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserAddress> userAddressList = userAddressMapper.selectList(queryWrapper);
        return userAddressList;
    }

    @Override
    public UserInfo login(UserInfo userInfo) {
        // select * from userInfo where userName = ? and passwd = ?
        // 注意密码是加密：
        String passwd = userInfo.getPasswd(); //123
        // 将passwd 进行加密
        String newPasswd = DigestUtils.md5DigestAsHex(passwd.getBytes());

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", userInfo.getLoginName());
        queryWrapper.eq("passwd", newPasswd);
        UserInfo info = userInfoMapper.selectOne(queryWrapper);
        if (info != null) {

            return info;
        }
        return null;
    }
}
